<?php
class UsersAction extends Action {
	public function index() {
		if (!$this->isLogin()) {
			$this->redirect('/Admin/Public/login');
		} 	
		
		$agencyNameList = D('Agency')->getAllAgencyName();
		
		$this->assign('agencyList', $agencyNameList);
		$this->display();
	}
	
	public function loadUsersPage() {
		$nowPage = isset($_POST['p'])?$_POST['p']:1;
		//search condition
		$semail = isset($_POST['semail'])?$_POST['semail']:'';
		$sfName = isset($_POST['sfName'])?$_POST['sfName']:'';
		$slName = isset($_POST['slName'])?$_POST['slName']:'';
		$sTime = isset($_POST['sTime'])?$_POST['sTime']:'';
		$eTime = isset($_POST['eTime'])?$_POST['eTime']:'';
		$createdby = isset($_POST['createdby'])?$_POST['createdby']:'';
		
		$sql = 'select u.uid, u.`account`, u.`firstname`, u.`lastname`, u.`email` , u.`gender`, u.`paid`,u.`confirmid`, u.`statusid`, u.`createtime`, a.name'; 
		$from = ' from pse_user_agency as ua '.
		' right join pse_users as u on ua.uid=u.uid '.
		' left join pse_agency as a on ua.agencyId=a.id ';
		$sql = $sql . $from;
		$where = '';
		if (!empty($semail)) {
			$where = " where u.email like '%$semail%'";
		}
		if (!empty($sTime)){
			if (empty($where)) {
				$where = " where createtime > '$sTime' "; 
			} else {
				$where = $where . " and createtime > '$sTime'";
			}
		}
		if (!empty($eTime)){
			if (empty($where)) {
				$where = " where createtime < '$eTime' ";
			} else {
				$where = $where . " and createtime < '$eTime'";
			}
		}
		if (!empty($createdby)) {
			if (empty($where)) {
				$where = " where a.name = '$createdby' ";
			} else {
				$where = $where . " and a.name = '$createdby'";
			}
		}
		if (!empty($sfName)) {
			if (empty($where)) {
				$where = " where u.firstname like '%$sfName%' ";
			} else {
				$where = $where . " and u.firstname like '%$sfName%' ";
			}
		}
		if (!empty($slName)) {
			if (empty($where)) {
				$where = " where u.lastname like '%$slName%' ";
			} else {
				$where = $where . " and u.lastname like '%$slName%'";
			}
		}
		
		$countSql = 'select count(*) c ' . $from . $where;
		
		$count = M()->query($countSql);
		$count = $count[0]['c'];	
		import('ORG.Util.Page');
		$Page = new Page($count, 15);
		$totalPages = $Page->getTotalPages();
		$firstRow = $Page->firstRow;
		$listRows = $Page->listRows;
		$pageInfo = array(totalPages => $totalPages, nowPage => $nowPage);
		
		$where = $where . ' order by createtime desc';
		$where = $where . " limit $firstRow, $listRows";
		$sql = $sql . $where;
		
		//$list = $Users->where($map)->order($o)->page($nowPage.','.$Page->listRows)->select();
		$list = M()->query($sql);
		
		if ($list === false) {
			jsonecho(0, L('load_data_failed'));
		} else if (empty($list)) {
			jsonecho(2, L('no_data'));
		} else {
			echo json_encode(array(status => 1, data => $list, page => $pageInfo));
		}
	}
	
	public function getUserInfo() {
		$uid = $this->_post('uid');
		$user = D('Users')->getUserInfoById($uid);
		$user['birthday'] = substr($user['birthday'], 0, 11);
		$user['deathday'] = substr($user['deathday'], 0, 11);
		if (!empty($user)) {
			echo json_encode(array(status=>1, data=>$user));
		}	
	}
	
	public function add() {
		if (!$this->isLogin()) {
			$this->redirect('/Admin/Public/login');
		}
		$this->display();  
	}
	
	public function doAdd() {
		if (!$this->isLogin()) {
			$this->redirect('/Admin/Public/login');
		}
		$Users = D('Users');
		if (!$Users->create()) {
			$errorMsg = $Users->getError();
			$this->error(L($errorMsg));
		}
		
		$account = $this->_post('account');
		if($Users->accountExisted($account)) {
			$this->error(L('email_existed'));
		}
		$data['account'] = $account;
		$data['firstname'] = $this->_post('firstname');
		$data['secondname'] = $this->_post('secondname');
		$data['lastname'] = $this->_post('lastname');
		$data['email'] =  $this->_post('email');
		$data['password'] = md5($this->_post('password'));
		$data['gender'] = $this->_post('gender');
		$data['avatar']= $this->_post('avatar');
		$data['birthday'] = $this->_post('birthday');
		$data['deathday'] = $this->_post('deathday');
		$data['twodimcode'] = $this->_post('twodimcode');
		$data['visiable'] = $this->_post('visiable');
		$data['relationship'] = $this->_post('relationship');
		$data['accesspwd'] = $this->_post('accesspwd');
		$data['pwdhint'] = $this->_post('pwdhint');
		$data['paid'] = $this->_post('paid');
		$data['createdby'] = $this->_post('createdby');
		$data['confirmid'] = $this->_post('confirmid');
		$data['statusid'] = $this->_post('statusid');
		$data['createtime'] = date('Y-m-d h:i:s', time());
		
		$result = $Users->add($data);
		print_r($data);
		
		
		if ($result === false) {
			$this->error(L('add_failed'));
		}
		
		$this->redirect('/Admin/Users/index');
	}
	
	public function doEdit() {
		if (!$this->isLogin()) {
			$this->redirect('/Admin/Public/login');
		}
		$Users = D('Users');
		if (!$Users->create()) {
			$errorMsg = $Users->getError();
			jsonecho(2, L($errorMsg));
		}
		
		$uid = $this->_post('uid');
		if (!isset($uid)) {
			jsonecho (3, L('edit_failed'));
		}
		
		$account = $this->_post('account');
		if(empty($account))  {
			jsonecho(1,L('email_require'));
		}

		$account = $this->_post('account');
		$tmpAccount = $Users->getUserAccount($uid);
		if ($tmpAccount != $account){
			if($Users->accountExisted($account)) {
				jsonecho (4, L('email_existed'));
			}
		}
	
		$newPassword = $this->_post('newPassword');
		if (!empty($newPassword)) {
			$data['password'] = md5($newPassword);
		}
		
		$data['uid'] = $uid;
		$data['account'] = $account;
		$data['firstname'] = $this->_post('firstname');
		$data['secondname'] = $this->_post('secondname');
		$data['lastname'] = $this->_post('lastname');
		$data['email'] = $this->_post('email');
		$data['gender'] = $this->_post('gender');
		$data['avatar']= $this->_post('avatar');
		$data['birthday'] = $this->_post('birthday');
		$data['deathday'] = $this->_post('deathday');
		$data['twodimcode'] = $this->_post('twodimcode');
		$data['visiable'] = $this->_post('visiable');
		$data['relationship'] = $this->_post('relationship');
		$data['accesspwd'] = $this->_post('accesspwd');
		
		$data['pwdhint'] = $this->_post('pwdhint');
		$data['paid'] = $this->_post('paid');
		$data['createdby'] = $this->_post('createdby');
		$data['confirmid'] = $this->_post('confirmid');
		$data['statusid'] = $this->_post('statusid');
	
		$result = $Users->save($data);
		if ($result === false) {
			jsonecho(5, L('edit_failed'));
		}
	
		jsonecho(0);
	}
	
	public function isLogin() {
		return  isset($_SESSION[C('ADMIN_AUTH_KEY')]) ? true: false;
	}
	
	function doDelete() {
		$ids = $_POST['ids'];
		$result = D('Users')->deleteUsersByIds($ids);
		if ($result === false) {
			jsonecho(0, L('delete_failed'));
		} else {
			jsonecho(1);
		}
	}
}